Skip to content

Method: static {...}

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions.change;
16:
17: import java.util.Iterator;
18: import java.util.Map;
19:
20: class MapChangeDetector implements ChangeDetector {
21:
22: private final ChangeDetector changeDetector;
23: private final ChangeManagerImpl changeManager;
24:
25: public MapChangeDetector(ChangeDetector changeDetector, ChangeManagerImpl changeManager) {
26: this.changeDetector = changeDetector;
27: this.changeManager = changeManager;
28: }
29:
30: @Override
31: public Changed hasChanges(Object clone, Object original) {
32: assert clone != null;
33: assert original != null;
34:
35: final Map<?, ?> cl = (Map<?, ?>) clone;
36: final Map<?, ?> orig = (Map<?, ?>) original;
37: if (orig.size() != cl.size()) {
38: return Changed.TRUE;
39: }
40: boolean changes = false;
41: final Iterator<?> it = orig.keySet().iterator();
42: while (it.hasNext() && !changes) {
43: final Object origKey = it.next();
44: if (!cl.containsKey(origKey)) {
45: return Changed.TRUE;
46: }
47: // TODO Maybe we should check also for key changes
48: final Object origVal = orig.get(origKey);
49: Object clVal = cl.get(origKey);
50:
51: final Changed ch = changeDetector.hasChanges(origVal, clVal);
52: switch (ch) {
53: case TRUE:
54: return ch;
55: case FALSE:
56: break;
57: case UNDETERMINED:
58: changes = changeManager.hasChangesInternal(origVal, clVal);
59: }
60: }
61: return Changed.fromBoolean(changes);
62: }
63: }